package com.supertange.miniapp.wepet.modules.wepet.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.supertange.miniapp.wepet.common.idworker.Sid;
import com.supertange.miniapp.wepet.common.utils.Assert;
import com.supertange.miniapp.wepet.common.vo.AjaxResult;
import com.supertange.miniapp.wepet.modules.wepet.entity.TopicAnswerEntity;
import com.supertange.miniapp.wepet.modules.wepet.entity.TopicCommentEntity;
import com.supertange.miniapp.wepet.modules.wepet.entity.TopicEntity;
import com.supertange.miniapp.wepet.modules.wepet.entity.UsersEntity;
import com.supertange.miniapp.wepet.modules.wepet.entity.vo.*;
import com.supertange.miniapp.wepet.modules.wepet.mapper.TopicAnswerMapper;
import com.supertange.miniapp.wepet.modules.wepet.mapper.TopicCommentMapper;
import com.supertange.miniapp.wepet.modules.wepet.mapper.TopicMapper;
import com.supertange.miniapp.wepet.modules.wepet.mapper.UsersMapper;
import com.supertange.miniapp.wepet.modules.wepet.service.TopicService;
import com.supertange.miniapp.wepet.modules.wepet.service.UserService;
import com.supertange.miniapp.wepet.modules.wepet.utils.TopicUtil;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.listener.Topic;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service("topicService")
public class TopicServiceImpl implements TopicService {

    @Autowired
    private UsersMapper userMapper;

    @Autowired
    private TopicMapper topicMapper;

    @Autowired
    private TopicAnswerMapper topicAnswerMapper;

    @Autowired
    private TopicCommentMapper topicCommentMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private Sid sid;

    @Autowired
    private WxMaService wxMaService;



    @Override
    public List<TopicVO> queryTopicAll() {
        List<TopicVO> result = topicMapper.QueryAllTopic();
        if(result.size()<=6){
            return result;
        }else{
            return result.subList(0,5);
        }
    }

    @Override
    public List<TopicVO> queryTopicRefresh() {
        List<TopicVO> result = topicMapper.QueryAllTopic();
        int m = result.size()<=6?result.size():6;
        int n = new Random().nextInt(result.size());
//        int[] randomArray = TopicUtil.getRandom(0,result.size(),n);
        List<TopicVO> resultNew = new ArrayList<>();
        for(int i=n;i<result.size()&&i<n+m;i++){
            resultNew.add(result.get(i));
        }
        for(int i=0;i<m-resultNew.size();i++){
            resultNew.add(result.get(n-(i+1)));
        }
        return resultNew;
    }

    @Override
    public List<TopicVO> queryTopicById(String userid) {
        return topicMapper.queryTopicById(userid);
    }

    @Override
    public TopicVO queryTopicByTopicid(String topicid) {
        return topicMapper.queryTopicByTopicid(topicid);
    }

    @Override
    public  List<TopicEnterReturnVO> queryAllTopicAnswerById(String topicid){
        List<TopicEnterReturnVO> result = new ArrayList<>();
        List<TopicAnawerVO> topicAnawerVOList = topicAnswerMapper.queryTopicAnswer(topicid);
        for(int i=0;i<topicAnawerVOList.size();i++){
            TopicEnterReturnVO topicEnterReturnVO = new TopicEnterReturnVO();
            topicEnterReturnVO.setFollow_comment_list(topicCommentMapper.queryCommentById(topicAnawerVOList.get(i).getId()));
            topicEnterReturnVO.setMain_comment(topicAnawerVOList.get(i));
            result.add(topicEnterReturnVO);
        }
        return result;
    }

    @Override
    public List<TopicUserAnswerVO> queryUserAnswerByUserid(String userid) {
        return topicAnswerMapper.queryUserAnswerByUserid(userid);
    }

    @Override
    public List<TopicAnswerUserVO> queryAnswerUserByUserid(String userid) {
        return topicAnswerMapper.queryAnswerUserByUserid(userid);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public String addTopic(String userid,String title,String content,int piclen){
        UsersEntity usersEntity = userService.queryUserInfo(userid);
        if(usersEntity==null){
            return null;
        }
        TopicEntity topicEntity = new TopicEntity();
        topicEntity.setUserid(userid);
        topicEntity.setUsername(usersEntity.getUsername());
        Date now = new Date();
        SimpleDateFormat f= new SimpleDateFormat("yyyy年MM月dd日kk点mm分");
        topicEntity.setId(sid.nextShort());
        topicEntity.setDate(f.format(now));
        topicEntity.setFavourate_cnt(0);
        topicEntity.setTitle(title);
        topicEntity.setContent(content);
        topicEntity.setAnswer_cnt(0);
        topicEntity.setPiclen(piclen);
        topicMapper.insert(topicEntity);
        return topicEntity.getId();
    }

    @Override
    public boolean addPic(MultipartFile file, String topicid,int index) {
        File img = TopicUtil.qnImageUpload(file,topicid,index);
        if(img==null)
            return false;
        try {
            if(wxMaService.getSecCheckService().checkImage(img)){
                return true;
            }else{
                img.delete();
            }
            return false;
        } catch (WxErrorException e) {
            e.printStackTrace();
            img.delete();
            return false;
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean addTopicAnswer(String userId,String topicId,String comment){
        UsersEntity usersEntity = userService.queryUserInfo(userId);
        if(userId==null)
            return false;
        TopicEntity topicEntity = topicMapper.selectById(topicId);
        if(topicEntity==null)
            return false;
        TopicAnswerEntity topicAnswerEntity = new TopicAnswerEntity();
        topicAnswerEntity.setId(sid.nextShort());
        Date now = new Date();
        SimpleDateFormat f= new SimpleDateFormat("yyyy年MM月dd日kk点mm分");
        topicAnswerEntity.setDate(f.format(now));
        topicAnswerEntity.setUserid(userId);
        topicAnswerEntity.setTopicid(topicId);
        topicAnswerEntity.setComment(comment);
        topicAnswerEntity.setFavourate_cnt(0);
        topicAnswerMapper.insert(topicAnswerEntity);
        topicMapper.addTopicAnswerCnt(topicId);
        return true;
    }

    @Override
    public boolean addTopicComment(String userId,int commentId,int type,String comment,String answerid,String commentUsername){
        UsersEntity usersEntity = userService.queryUserInfo(userId);
        if(usersEntity==null)
            return false;
        TopicAnswerEntity topicAnswerEntity = topicAnswerMapper.selectById(answerid);
        if(topicAnswerEntity==null)
            return false;
        TopicCommentEntity topicCommentEntity = new TopicCommentEntity();
        topicCommentEntity.setAnswerid(answerid);
        topicCommentEntity.setComment(comment);
        topicCommentEntity.setCommentid(commentId);
        topicCommentEntity.setType(type);
        topicCommentEntity.setUserid(userId);
        topicCommentEntity.setCommentUsername(commentUsername);
        topicCommentMapper.insert(topicCommentEntity);
        return true;
    }

    @Override
    public void addTopicFavourate(String topicid){
        topicMapper.addTopicFavourate(topicid);
    }

    @Override
    public void subTopicFavourate(String topicid){
        topicMapper.subTopicFavourate(topicid);
    }

    @Override
    public void addTopicAnswerFavourate(String answerid) {
        topicAnswerMapper.addTopiAnswerFavourate(answerid);
    }


}
